package tenumn.energytech.registrys.capability;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Mth;
import net.minecraftforge.common.util.INBTSerializable;

public class CapabilityHeart implements ICapabilityHeart,INBTSerializable<CompoundTag> {
    private final float min;
    private final float max;
    private float temperature;

    public CapabilityHeart(float temperature, float min, float max) {
        this.temperature = temperature;
        this.min = min;
        this.max = max;
    }

    public CapabilityHeart(float temperature) {
        this(temperature, Float.MIN_VALUE, Float.MAX_VALUE);
    }

    public CapabilityHeart() {
        this(0);
    }

    public float getTemperature() {
        return temperature;
    }

    public void setTemperature(float temperature) {
        this.temperature = Mth.clamp(temperature, min, max);
    }
    public float rise(float amount, boolean simulate) {
        float rised=Math.min(max-temperature,amount);
        if(!simulate)temperature+=rised;
        return rised;
    }
    public float coolDown(float amount, boolean simulate) {
        float cooled=Math.min(temperature,amount);
        if(!simulate)temperature-=cooled;
        return cooled;
    }

    public float getMin() {
        return min;
    }

    public float getMax() {
        return max;
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag=new CompoundTag();
         tag.putFloat("Temperature",temperature);
         return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
        setTemperature(nbt.getFloat("Temperature"));
    }

    public boolean canReceive() {
        return true;
    }
}
